<?php
/*
	This SQL query will create the table to store your object.

	CREATE TABLE `documentgotprorolemap` (
	`documentid` int(11) NOT NULL,
	`gotproroleid` int(11) NOT NULL,INDEX(`documentid`, `gotproroleid`)) ENGINE=MyISAM;
*/

/**
* <b>DocumentGotProRoleMap</b> class with integrated CRUD methods.
* @author Php Object Generator
* @version POG 3.0f / PHP5.1 MYSQL
* @copyright Free for personal & commercial use. (Offered under the BSD license)
*/
class DocumentGotProRoleMap
{
	public $documentId = '';

	public $gotproroleId = '';

	public $pog_attribute_type = array(
		"documentId" => array('db_attributes' => array("NUMERIC", "INT")),
		"gotproroleId" => array('db_attributes' => array("NUMERIC", "INT")));
		public $pog_query;
	
	
	/**
	* Creates a mapping between the two objects
	* @param Document $object 
	* @param GotProRole $otherObject 
	* @return 
	*/
	function AddMapping($object, $otherObject)
	{
		if ($object instanceof Document && $object->documentId != '')
		{
			$this->documentId = $object->documentId;
			$this->gotproroleId = $otherObject->gotproroleId;
			return $this->Save();
		}
		else if ($object instanceof GotProRole && $object->gotproroleId != '')
		{
			$this->gotproroleId = $object->gotproroleId;
			$this->documentId = $otherObject->documentId;
			return $this->Save();
		}
		else
		{
			return false;
		}
	}
	
	
	/**
	* Removes the mapping between the two objects
	* @param Object $object 
	* @param Object $object2 
	* @return 
	*/
	function RemoveMapping($object, $otherObject = null)
	{
		$connection = Database::Connect();
		if ($object instanceof Document)
		{
			$this->pog_query = "delete from `documentgotprorolemap` where `documentid` = '".$object->documentId."'";
			if ($otherObject != null && $otherObject instanceof GotProRole)
			{
				$this->pog_query .= " and `gotproroleid` = '".$otherObject->gotproroleId."'";
			}
		}
		else if ($object instanceof GotProRole)
		{
			$this->pog_query = "delete from `documentgotprorolemap` where `gotproroleid` = '".$object->gotproroleId."'";
			if ($otherObject != null && $otherObject instanceof Document)
			{
				$this->pog_query .= " and `documentid` = '".$otherObject->documentId."'";
			}
		}
		Database::NonQuery($this->pog_query, $connection);
	}
	
	
	/**
	* Physically saves the mapping to the database
	* @return 
	*/
	function Save()
	{
		$connection = Database::Connect();
		$this->pog_query = "select `documentid` from `documentgotprorolemap` where `documentid`='".$this->documentId."' AND `gotproroleid`='".$this->gotproroleId."' LIMIT 1";
		$rows = Database::Query($this->pog_query, $connection);
		if ($rows == 0)
		{
			$this->pog_query = "insert into `documentgotprorolemap` (`documentid`, `gotproroleid`) values ('".$this->documentId."', '".$this->gotproroleId."')";
		}
		return Database::InsertOrUpdate($this->pog_query, $connection);
	}
}
?>